import { createRouter, createWebHistory } from 'vue-router'
import axios from "axios";
import {ElMessage} from "element-plus";

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path:'/',
            redirect:'/login'
        },
        {
            path:'/login',
            component:()=>import('@/views/Login.vue'),
        },
        {
            path: '/admin/index',
            component:()=>import('@/views/admin/Index.vue'),
            children: [ //嵌套子路由
                {
                    path: '',
                    component: () => import('@/views/admin/HomeView.vue')
                },
                {
                    path: '/admin/usermanage',
                    component: () => import('@/views/admin/UserManage.vue')
                },
                {
                    path: '/admin/DepartmentAndSpecializedGroup',
                    component: () => import('@/views/admin/DepartmentAndSpecializedGroup.vue')
                },
                {
                    path: '/admin/hosmanage',
                    component: () => import('@/views/admin/HospitalManage.vue')
                },
                {
                    path: '/admin/adminmanage',
                    component: () => import('@/views/admin/AdminManage.vue')
                },
                {
                    path: '/admin/Announcement',
                    component: () => import('@/views/admin/Announcement.vue')
                }
            ]
        },
        {
            path: '/hosadmin/index',
            component:()=>import('@/views/hosadmin/HosIndex.vue'),
            children: [ //嵌套子路由
                {
                    path: '',
                    component: () => import('@/views/hosadmin/HomeView.vue')
                },
                {
                    path: '/hosadmin/hdepartmentManage',
                    component: () => import('@/views/hosadmin/hdepartmentManage.vue')
                },
                {
                    path: '/hosadmin/offerNumberManage',
                    component: () => import('@/views/hosadmin/OfferNumberManage.vue')
                },
                {
                    path: '/hosadmin/inspect',
                    component: () => import('@/views/hosadmin/Inspect.vue')
                },
                {
                    path: '/hosadmin/ApprovedClinic',
                    component: () => import('@/views/hosadmin/ApprovedClinic.vue')
                },
                {
                    path: '/hosadmin/gerenxinxi',
                    component: () => import('@/views/hosadmin/GeRenxinxi.vue')
                },
                {
                    path: '/hosadmin/scheduling',
                    component: () => import('@/views/hosadmin/SchedulingPage.vue')
                },
                {
                    path: '/hosadmin/doctorManage',
                    component: () => import('@/views/hosadmin/DoctorManage.vue')
                },
                {
                    path: '/hosadmin/StatisticsView',
                    component: () => import('@/views/hosadmin/StatisticsView.vue')
                },
                {
                    path: '/hosadmin/HosMessage',
                    component: () => import('@/views/hosadmin/HosMessage.vue')
                },
            ]
        },
        {
            path: '/doctor/index',
            component:()=>import('@/views/doctor/Index.vue'),
            children: [ //嵌套子路由
                {
                    path: '',
                    component: () => import('@/views/doctor/HomeView.vue')
                },
                {
                    path: '/doctor/myscheduling',
                    component: () => import('@/views/doctor/MyScheduling.vue')
                },
                {
                    path: '/doctor/diagnosis',
                    component: () => import('@/views/doctor/Diagnosis.vue')
                },
                {
                    path: '/doctor/DiscontinuationView',
                    component: () => import('@/views/doctor/DiscontinuationView.vue')
                }
            ]
        }
    ]
})
router.beforeEach((to, from, next) => {
    if (to.path == '/login') return next()
    if (to.path.startsWith('/admin') || to.path.startsWith('/hosadmin')) {
        //从sessionStorage取得token代表jwt长串
        var token  = sessionStorage.getItem("adminToken");
        if (!token) {//token不存在,代表未登录
            ElMessage.error("请先登录")
            return next('/login')//跳转到登录页面
        } else {//token存在,代表登录
            const user = JSON.parse(sessionStorage.getItem("user"))
            if (to.path.startsWith('/admin') && (user.permission != 0 && user.permission != 1)){
                return next('/login')
            }
            if (to.path.startsWith('/hosadmin') && (user.permission != 2 && user.permission != 3 && user.permission != 4)){
                return next('/login')
            }
            //检验jwt的合法性
            axios({
                url: 'http://localhost:8080/admin/checkToken',
                method: 'get',
                headers: {
                    token: token //请求头加token:jwt长串
                }
            }).then(resp => {
                if (!resp.data) {
                    sessionStorage.removeItem("adminToken")
                    sessionStorage.removeItem("user")
                    return  next({path: '/login'})
                }
                next()//放行
            })
        }
    } else if (to.path.startsWith('/doctor')) {
        //从sessionStorage取得token代表jwt长串
        var token  = sessionStorage.getItem("token");
        if (!token) {//token不存在,代表未登录
            return next('/login')//跳转到登录页面
        } else {//token存在,代表登录
            //检验jwt的合法性
            axios({
                url: 'http://localhost:8080/doctor/checkToken',
                method: 'get',
                headers: {
                    token: token //请求头加token:jwt长串
                }
            }).then(resp => {
                if (!resp.data) {
                    ElMessage("登录已过期,请重新登录")
                    return  next({path: '/doctor/login'})
                }
                next()//放行
            })

        }
    }
})
export default router